//
//  MXHomeNavView.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/1.
//

#import "MXHomeNavView.h"
#import "MXHomeNavCell.h"

@interface MXHomeNavView () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *listCV;
@property (nonatomic,strong) NSArray *imageArr;
@property (nonatomic,strong) NSArray *titleArr;
@end

@implementation MXHomeNavView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 6;
    self.layer.masksToBounds = YES;
    self.imageArr = @[@"home_suggestion",@"home_notice",@"home_circle",@"home_car"];
    self.titleArr = @[@"喂食建议",@"提醒",@"社区",@"商城"];
    [self addSubview:self.listCV];
    [self.listCV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(5);
        make.right.offset(-5);
        make.top.offset(14);
        make.bottom.offset(-11);
    }];
}

- (UICollectionView *)listCV {
   if (!_listCV) {
       UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
       layout.itemSize = CGSizeMake((kScreenW - 10-30) / 4.0, 46);
       layout.minimumLineSpacing = 0;//纵向间距(参考scrollDirection)
       layout.minimumInteritemSpacing = 0;//横向间距
       layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
       _listCV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
       _listCV.delegate = self;
       _listCV.dataSource = self;
       _listCV.showsVerticalScrollIndicator = NO;
       _listCV.showsHorizontalScrollIndicator = NO;
       [_listCV registerClass:[MXHomeNavCell class] forCellWithReuseIdentifier:@"MXHomeNavCell"];
       
       }
    return _listCV;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MXHomeNavCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MXHomeNavCell" forIndexPath:indexPath];
    [cell reloadCellWithIconName:self.imageArr[indexPath.item] title:self.titleArr[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellClick) {
        self.cellClick(indexPath.item);
    }
}

@end
